﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for clsTableAction
/// </summary>
public class clsTableAction : clsConnection
{
    private string _tableName;
	public clsTableAction(string tableName)
	{
        _tableName = tableName;
	}
    //Dua du lieu vao DataReader
    public SqlDataReader exportDataReader(string cols, string cri)
    {
        try
        {
            connectDatabase();
            string sql = "select " + cols + "  from " + _tableName + ((cri.Length==0)? "" : " where " + cri).ToString();
            SqlCommand sqlComm = new SqlCommand();
            sqlComm.Connection = _sqlConn;
            sqlComm.CommandType = System.Data.CommandType.Text;
            sqlComm.CommandText = sql;
            return (sqlComm.ExecuteReader());
        }
        catch
        {
            return null;
        }
    }
    public DataTable exportDataTable(string cols, string cri)
    {
        try
        {
            connectDatabase();
            DataTable dtTable = new DataTable();
            string sql = "select " + cols + "  from " + _tableName + ((cri.Length == 0) ? "" : " where " + cri).ToString();
            SqlDataAdapter sqlDaAd = new SqlDataAdapter(sql, _sqlConn);
            sqlDaAd.Fill(dtTable);
            return (dtTable);
        }
        catch
        {
            return null;
        }
    }
    //Dua du lieu vao luoi
    public void exportDataToGridview(ref GridView gv, string cols, string cri)
    {
        SqlDataReader sqlDare = this.exportDataReader(cols, cri);
        gv.DataSource = sqlDare;
        gv.DataBind();
    }
    //Dua du lieu vao luoi
    public void exportDataToGridviewPaging(int pageIndex, int pageSize, ref GridView gv, string cols, string cri)
    {
        DataTable dtTable = this.exportDataTable(cols, cri);
        gv.DataSource = dtTable;
        gv.AllowPaging = true;
        gv.PageSize = pageSize;
        gv.PageIndex = pageIndex;
        gv.DataBind();
    }
    public void exportDataToDropDownList(ref DropDownList drop, string txt, string val, string cri)
    {
        SqlDataReader sqlDare = this.exportDataReader(txt + ", " + val, cri);
        drop.DataSource = sqlDare;
        drop.DataTextField = txt;
        drop.DataValueField = val;
        drop.DataBind();
    }
    public void exportDataToListBox(ref ListBox lst, string txt, string val, string cri)
    {
        SqlDataReader sqlDare = this.exportDataReader(txt + ", " + val, cri);
        lst.DataSource = sqlDare;
        lst.DataTextField = txt;
        lst.DataValueField = val;
        lst.DataBind();
    }
    public void exportDataToCheckBoxList(ref CheckBoxList lst, string txt, string val, string cri)
    {
        SqlDataReader sqlDare = this.exportDataReader(txt + ", " + val, cri);
        lst.DataSource = sqlDare;
        lst.DataTextField = txt;
        lst.DataValueField = val;
        lst.DataBind();
    }
}
